<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="用户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysUserId">
              <j-search-select-tag type="list" v-model="model.sysUserId" dict="sys_user,realname,id" placeholder="请选择用户" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item v-has="'mycoupon:phone'" label="电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="自习室" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zixishiId">
              <j-dict-select-tag type="list" v-model="model.zixishiId" dictCode="sr_zixishi,name,id" placeholder="请选择自习室" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="区域" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quyuId">
              <j-dict-select-tag type="list" v-model="model.quyuId" dictCode="sr_quyu,name,id" placeholder="请选择区域" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponName">
              <a-input v-model="model.couponName" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="原金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="origAmt">
              <a-input-number v-model="model.origAmt" placeholder="请输入原金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="优惠金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="disAmt">
              <a-input-number v-model="model.disAmt" placeholder="请输入优惠金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="有效期开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="effectStartDate">
              <j-date placeholder="请选择有效期开始时间" v-model="model.effectStartDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="有效期结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="effectEndDate">
              <j-date placeholder="请选择有效期结束时间" v-model="model.effectEndDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="开始使用时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useStartTime">
              <j-time placeholder="请选择开始使用时间"  v-model="model.useStartTime" :show-time="true" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="结束使用时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useEndTime">
              <j-time placeholder="请选择结束使用时间"  v-model="model.useEndTime" :show-time="true" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="截止日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <j-date placeholder="请选择截止日期"  v-model="model.endDate" :show-time="true" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="使用状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useStatus">
              <j-dict-select-tag type="list" v-model="model.useStatus" dictCode="coupon_use_status" placeholder="请选择使用状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="使用时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useTime">
              <j-date placeholder="请选择使用时间"  v-model="model.useTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="时长" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="duration">
              <a-input-number v-model="model.duration" placeholder="请输入时长" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="剩余时长" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="durationLeft">
              <a-input-number v-model="model.durationLeft" placeholder="请输入时长" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="couponType">
              <j-dict-select-tag type="list" v-model="model.couponType" dictCode="coupon_type" placeholder="请选择类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="多次使用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="multipleFlag">
              <j-dict-select-tag type="list" v-model="model.multipleFlag" dictCode="yn" placeholder="请选择是否允许多次使用" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MyCouponForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           zixishiId: [
              { required: true, message: '请输入自习室!'},
           ],
           quyuId: [
              { required: true, message: '请输入区域!'},
           ],
           couponName: [
              { required: true, message: '请输入名称!'},
           ],
           origAmt: [
              { required: true, message: '请输入原金额!'},
              { pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
           ],
           disAmt: [
              { required: true, message: '请输入优惠金额!'},
              { pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
           ],
        },
        url: {
          add: "/coupon/myCoupon/add",
          edit: "/coupon/myCoupon/edit",
          queryById: "/coupon/myCoupon/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>